package com.ruoyi.web.controller.system;

import java.util.HashMap;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysThesauriWordReference;
import com.ruoyi.system.service.ISysThesauriWordReferenceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 叙词引用关系Controller
 * 
 * @author ruoyi
 * @date 2022-08-24
 */
@RestController
@RequestMapping("/system/thesauriWordReference")
public class SysThesauriWordReferenceController extends BaseController
{
    @Autowired
    private ISysThesauriWordReferenceService sysThesauriWordReferenceService;

    /**
     * 查询叙词引用关系列表
     */
    @PreAuthorize("@ss.hasAnyPermi('system:thesauriWordReference:list,system:thesauriWord:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysThesauriWordReference sysThesauriWordReference)
    {
        startPage();
        List<SysThesauriWordReference> list = sysThesauriWordReferenceService.selectSysThesauriWordReferenceList(sysThesauriWordReference);
        return getDataTable(list);
    }

    /**
     * 导出叙词引用关系列表
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordReference:export')")
    @Log(title = "叙词引用关系", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysThesauriWordReference sysThesauriWordReference)
    {
        List<SysThesauriWordReference> list = sysThesauriWordReferenceService.selectSysThesauriWordReferenceList(sysThesauriWordReference);
        ExcelUtil<SysThesauriWordReference> util = new ExcelUtil<SysThesauriWordReference>(SysThesauriWordReference.class);
        return util.exportExcel(list, "叙词引用关系数据");
    }

    /**
     * 获取叙词引用关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordReference:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysThesauriWordReferenceService.selectSysThesauriWordReferenceById(id));
    }

    /**
     * 新增叙词引用关系
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordReference:add')")
    @Log(title = "叙词引用关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysThesauriWordReference sysThesauriWordReference)
    {
        return toAjax(sysThesauriWordReferenceService.insertSysThesauriWordReference(sysThesauriWordReference));
    }

    /**
     * 新增叙词引用关系
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordReference:add')")
    @Log(title = "叙词引用关系", businessType = BusinessType.INSERT)
    @PostMapping("/multivalued")
    public AjaxResult addMultivalued(@RequestBody HashMap<String, Object> params){
        String thesauriWordId = params.get("thesauriWordId") + "";
        String referenceRelationLabel = params.get("referenceRelationLabel") + "";
        String thesauriWordModuleId = params.get("thesauriWordModuleId") + "";
        List<Object> referenceWordIds = (List<Object>)params.get("referenceWordIds");
        SysThesauriWordReference thesauriWordReference = null;
        for (Object referenceWordId : referenceWordIds) {
            thesauriWordReference = new SysThesauriWordReference();
            thesauriWordReference.setThesauriWordId(thesauriWordId);
            thesauriWordReference.setReferenceRelationLabel(referenceRelationLabel);
            thesauriWordReference.setThesauriWordModuleId(Long.parseLong(thesauriWordModuleId));
            thesauriWordReference.setReferenceWordId(referenceWordId+"");
            List<SysThesauriWordReference> sysThesauriWordReferencesList = sysThesauriWordReferenceService.selectSysThesauriWordReferenceList(thesauriWordReference);
            if(sysThesauriWordReferencesList.size() == 0){
                sysThesauriWordReferenceService.insertSysThesauriWordReference(thesauriWordReference);
            }
        }
        return success();
    }

    /**
     * 修改叙词引用关系
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordReference:edit')")
    @Log(title = "叙词引用关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysThesauriWordReference sysThesauriWordReference)
    {
        return toAjax(sysThesauriWordReferenceService.updateSysThesauriWordReference(sysThesauriWordReference));
    }

    /**
     * 删除叙词引用关系
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordReference:remove')")
    @Log(title = "叙词引用关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysThesauriWordReferenceService.deleteSysThesauriWordReferenceByIds(ids));
    }
}
